package com.quanshu.exchange.ui.gui.qrcode;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baselibrary.util.glide.GlideUtils;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.UserInterface;
import com.quanshu.exchange.support.entity.UserEntity;
import com.quanshu.exchange.support.net.ApiException;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.AppUtils;
import com.quanshu.exchange.support.utils.CashierInputFilter;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.gui.message.chat.redpackage.DialogCheckPayPwd;
import com.quanshu.exchange.ui.gui.profile.SetPayPasswordActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PayTokenActivity extends BaseActivity {

    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.user_avatar)
    ImageView userAvatar;
    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.input_token)
    EditText inputToken;
    @BindView(R.id.ok_btn)
    TextView okBtn;

    private DialogCheckPayPwd checkPayPwdDialog;

    private String inputNum;

    private UserEntity userEntity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_token);
        ButterKnife.bind(this);
        initIntent();
        initView();
        initListener();
    }

    private void initIntent() {
        userEntity = (UserEntity) getIntent().getSerializableExtra("UserEntity");
    }

    private void initView() {
        if (userEntity != null) {
            GlideUtils.setRoundImageView(this, userEntity.getAvatar(), R.drawable.default_avatar, userAvatar);
            userName.setText("付款给:" + AppUtils.getUserName(userEntity));
        }
        CashierInputFilter[] filters = {new CashierInputFilter()};
        inputToken.setFilters(filters);
    }

    private void initListener() {
        RxViewHelper.getInstance().clicks(back, aVoid -> finish());
        RxViewHelper.getInstance().clicks(okBtn, aVoid -> doClickPayBtn());
        inputToken.addTextChangedListener(new MyTextWatcher());
    }

    private void doClickPayBtn() {
        checkPayPwdDialog = new DialogCheckPayPwd(this);
        checkPayPwdDialog.show();
        checkPayPwdDialog.reset();
        checkPayPwdDialog.setPointsNumText(inputNum);
        checkPayPwdDialog.getOkSubject().subscribe(str -> doCheck(str));
    }

    private void doCheck(String str) {

        NetClient.getInstance()
                .getNetService(UserInterface.class)
                .payTokenByQrCode(AppUtils.getSignParams(currentUser.getMemberid(), userEntity.getMemberid(), inputNum, str),
                        currentUser.getMemberid(), userEntity.getMemberid(), inputNum, str)
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        if (checkPayPwdDialog != null) {
                            checkPayPwdDialog.reset();
                            checkPayPwdDialog.cancel();
                        }
                        finish();
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        super.onFailure(e);
                        if (checkPayPwdDialog != null) {
                            checkPayPwdDialog.reset();
                            checkPayPwdDialog.cancel();
                        }
                        ApiException apiException = (ApiException) e;
                        // 如果返回未设置交易密码
                        if (apiException.getCode() == 1005) {
                            Intent intent = new Intent(PayTokenActivity.this, SetPayPasswordActivity.class);
                            intent.putExtra("setPwd", true);
                            startActivityForResult(intent, 0x01);
                        }
                    }
                });
    }

    private class MyTextWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            inputNum = inputToken.getText().toString();
            if (inputNum != null && inputNum.length() > 0) {
                okBtn.setEnabled(true);
            } else {
                okBtn.setEnabled(false);
            }
        }
    }

}
